﻿using BCCommon;
using BCCommon.TMS.FreightEnums;
using BCCommon.TMS.Order;
using BCDto.TMS.FreightConfiguration;
using BCService.Common.Area;
using BCService.TMS.FreightConfigurationService;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace BCTMSRESTful.Controllers.Management.TMS.FreightConfiguration
{
    /// <summary>
    /// 运费配置
    /// </summary>
    [Route("api/v1/sim/[controller]")]
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    public class FreightConfigurationController : ManagementBaseController
    {
        #region 私有变量

        private readonly ICountyService countyService;
        private readonly IProvinceService provinceService;
        private readonly ICityService cityService;
        private readonly IFreightConfigurationService freightConfigurationService;

        #endregion 私有变量

        #region 构造函数

        public FreightConfigurationController(
            ICountyService countyService,
            IProvinceService provinceService,
            ICityService cityService,
            IFreightConfigurationService freightConfigurationService)
        {
            this.countyService = countyService;
            this.provinceService = provinceService;
            this.cityService = cityService;
            this.freightConfigurationService = freightConfigurationService;
        }

        #endregion 构造函数

        #region 公共方法

        /// <summary>
        /// 创建运费配置
        /// </summary>
        /// <param name="dto"></param>
        /// <response code="200">创建车辆成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(bool), 200)]
        [Log("创建运费配置")]
        public IActionResult CreateFreightConfig([FromBody] FreightConfigurationRequestDto dto)
        {
            //1 检查省是否存在
            CheckProvince(dto.ProvinceCode);
            //2 检查市是否存在
            CheckCity(dto.CityCode);
            //3 检查区/县是否存在
            CheckCounty(dto.CountyCode);
            var res = freightConfigurationService.InsertAsync(this.Admin.FullName, dto);
            return Ok(res);
        }

        /// <summary>
        /// 修改运费配置
        /// </summary>
        /// <param name="freightConfigurationId">运费配置id</param>
        /// <param name="dto">请求模型</param>
        /// <response code="201">数据修改成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpPut("{freight_configuration_id}")]
        [ProducesResponseType(typeof(bool), 201)]
        [Log("修改运费配置")]
        public IActionResult Put([FromRoute(Name = "freight_configuration_id")] int freightConfigurationId, [FromBody] FreightConfigurationRequestDto dto)
        {
            //1 检查省是否存在
            CheckProvince(dto.ProvinceCode);
            //2 检查市是否存在
            CheckCity(dto.CityCode);
            //3 检查区/县是否存在
            CheckCounty(dto.CountyCode);
            var res = freightConfigurationService.UpdateAsync(Admin.IsSystemAccount, this.Admin.FullName, freightConfigurationId, dto);
            return StatusCode((int)HttpStatusCode.Created, res);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="freightConfigurationId">运费配置id</param>
        /// <response code="204">数据删除成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpDelete("{freight_configuration_id}")]
        [ProducesResponseType(typeof(bool), 204)]
        [Log("删除运费配置")]
        public IActionResult Delete([FromRoute(Name = "freight_configuration_id")] int freightConfigurationId)
        {
            if (!Admin.IsSystemAccount)
                throw new ArgumentException("抱歉，您没有权限删除");
            var res = freightConfigurationService.DeleteAsync(freightConfigurationId);
            return StatusCode((int)HttpStatusCode.NoContent, res);
        }

        /// <summary>
        /// 通过id获取运费配置信息
        /// </summary>
        /// <param name="freightConfigurationId">运费配置id</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpGet("[action]/{freight_configuration_id}")]
        [ProducesResponseType(typeof(FreightConfigurationDto), 200)]
        [Log("通过id获取运费配置信息")]
        public IActionResult GetById([FromRoute(Name = "freight_configuration_id")] int freightConfigurationId)
        {
            var data = freightConfigurationService.GetByIdAsync(freightConfigurationId);
            return Ok(data);
        }

        /// <summary>
        /// 分页查询运费配置信息列表
        /// </summary>
        /// <param name="pageIndex">起始页</param>
        /// <param name="pageSize">页数量</param>
        /// <param name="provinceCode">省编码</param>
        /// <param name="cityCode">市编码</param>
        /// <param name="countyCode">区/县编码</param>
        /// <param name="vehicleTypeId">车辆类型id</param>
        /// <param name="sourceType">订单来源</param>
        /// <param name="freightType">物流类型</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<FreightConfigurationDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "province_code")] string provinceCode,
            [FromQuery(Name = "community_code")] string cityCode,
            [FromQuery(Name = "county_code")] string countyCode,
            [FromQuery(Name = "vehicle_type_id")] int? vehicleTypeId,
            [FromQuery(Name = "source_type")]OrderSourceType? sourceType,
            [FromQuery(Name = "freight_type")] FreightType? freightType,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var data = freightConfigurationService.GetAllAsync(this.Admin, new FreightConfigurationListRequestDto()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                ProvinceCode = provinceCode,
                CityCode = cityCode,
                VehicleTypeId = vehicleTypeId,
                CountyCode = countyCode,
                SourceType = sourceType,
                FreightType = freightType
            });
            return Ok(data);
        }

        /// <summary>
        /// 获取运费配置信息列表
        /// </summary>
        /// <param name="provinceCode">省编码</param>
        /// <param name="cityCode">市编码</param>
        /// <param name="countyCode">区/县编码</param>
        /// <param name="vehicleTypeId">车辆类型id</param>
        /// <param name="sourceType">订单来源</param>
        /// <param name="freightType">物流类型</param>
        /// <response code="200">数据查询成功。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<FreightConfigurationDto>), 200)]
        public IActionResult List(
            [FromQuery(Name = "province_code")] string provinceCode,
            [FromQuery(Name = "community_code")] string cityCode,
            [FromQuery(Name = "county_code")] string countyCode,
            [FromQuery(Name = "vehicle_type_id")] int? vehicleTypeId,
            [FromQuery(Name = "source_type")] OrderSourceType? sourceType,
            [FromQuery(Name = "freight_type")] FreightType? freightType)
        {
            var result = freightConfigurationService.GetList(this.Admin, provinceCode, cityCode, countyCode, vehicleTypeId, sourceType, freightType);
            return Ok(result);
        }

        /// <summary>
        /// 初始化修复不存在运费配置的区县
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult InitRepair()
        {
            var result = this.freightConfigurationService.InitRepair();
            return this.Ok(result);
        }

        #endregion 公共方法

        #region 私有方法

        /// <summary>
        /// 检查区/县是否存在
        /// </summary>
        /// <param name="countyCode"></param>
        private void CheckCounty(string countyCode)
        {
            var countyInfo = countyService.GetCountyByCode(countyCode);
            if (countyInfo == null)
                throw new ArgumentException("区/县不存在");
        }

        /// <summary>
        /// 检查市是否存在
        /// </summary>
        /// <param name="cityCode">市编码</param>
        private void CheckCity(string cityCode)
        {
            var commountyInfo = cityService.Get(cityCode);
            if (commountyInfo == null)
                throw new ArgumentException("市不存在");
        }

        /// <summary>
        /// 检查省是否存在
        /// </summary>
        /// <param name="provinceCode">省份编码</param>
        private void CheckProvince(string provinceCode)
        {
            var provinceInfo = provinceService.GetProvinceEntity(provinceCode);
            if (provinceInfo == null)
                throw new ArgumentException("省不存在");
        }

        #endregion 私有方法
    }
}
